home *** CD-ROM | disk | FTP | other *** search
- /***
-
- GetRotatedStringBitmap
-
- By Guy Fullerton (hedgeboy@kagi.com)
-
- This function takes a pascal string, rotates it 90 degrees in the desired
- direction and passes back a BitMap of the rotated string image.
-
- It is based upon some Apple sample source code called RotateString. The
- Apple code left some unsightly pixels along the edges of the BitMap,
- so I fixed it.
-
- You are free to use this any way you like.
-
- ***/
-
-
- #include "GetRotatedStringBitmap.h"
-
-
- /******************************/
- /*** GetRotatedStringBitmap ***/
- /******************************/
-
- OSErr GetRotatedStringBitmap( Str255 str, BitMap *destMap, RotationDirection direction )
- {
- GrafPort srcPort;
- BitMap srcMap;
- char *srcPtr, *destPtr;
- FontInfo f;
- short textHeightBits;
- short textHeightBytes;
- short textWidthBits;
- short textWidthBytes;
- short row, column;
- short texFont,texFace,texMode,texSize;
- short err;
- GrafPtr savePort;
-
- GetPort(&savePort);
-
- texFont = qd.thePort->txFont;
- texFace = qd.thePort->txFace;
- texMode = qd.thePort->txMode;
- texSize = qd.thePort->txSize;
-
- OpenPort( &srcPort);
- SetPort(&srcPort);
-
- TextFont(texFont);
- TextFace(texFace);
- TextMode(texMode);
- TextSize(texSize);
- GetFontInfo( &f);
-
- // calculate the height bits for the text. this is equal to the font's ascent plus descent
- textHeightBits = f.ascent + f.descent;// + f.leading;
-
- // calculate the height bytes. this is the number of bytes just equal to or greater than
- // the number of bits divided by 8
- textHeightBytes = (textHeightBits + 7) / 8;
-
- // calculate the width of the text, in bits
- textWidthBits = StringWidth( str );
-
- // calculate the width bytes just like we did for the height bytes
- textWidthBytes = (textWidthBits + 7) / 8;
-
- // create a new bit map to hold the source text
- srcPort.portBits.baseAddr = NewPtrClear( textWidthBytes * textHeightBits );
- if( err = MemError() ) return err;
-
- // set up the row bytes and bounds for the source bitmap
- srcPort.portBits.rowBytes = textWidthBytes;
- SetRect( &srcPort.portBits.bounds, 0, 0, textWidthBits, textHeightBits );
-
- // create the new bit map to hold the destination text
- destMap->baseAddr = NewPtrClear( textHeightBytes * textWidthBits );
- if( err = MemError() )
- {
- DisposePtr( srcPort.portBits.baseAddr );
- return err;
- }
-
- // set up the row bytes and bounds for the destination bitmap
- destMap->rowBytes = textHeightBytes;
- SetRect( &destMap->bounds, 0, 0, textHeightBits, textWidthBits );
-
- // draw the text into the source bitmap
- RectRgn( srcPort.visRgn, &srcPort.portBits.bounds );
- MoveTo( 0, f.ascent); // change 2.0.1 <CKH> was heightBits
- DrawString( str );
-
- srcPtr = srcPort.portBits.baseAddr; // Start at the Top,Left
-
- if(direction == rotationDirection_CounterClockWise) // Start at the Bottom,Left
- destPtr = destMap->baseAddr + ((textWidthBits - 1) * textHeightBytes);
- else // Start at the Top,Left
- destPtr = destMap->baseAddr;
-
- for( column = 0; column < textWidthBits; column++ ) /* width is in pixels */
- {
- for( row = 0; row < textHeightBits; row++ ) /* height in pixels, too */
- {
- if( direction == rotationDirection_CounterClockWise )
- {
- /* We are rotating left ( counter clock-wise ) thus */
- /* as we search across the top of the horizontal pixel bit map, */
- /* we write down the right edge of the vertical pixel bit map */
-
- if( *(srcPtr /* Begin with the upper left corner */
- + (row * textWidthBytes) /* Move down a row */
- + (column >> 3)) /* Go to the first even byte */
- & (0x80 >> (column & 7)) ) /* calculate a shifted bit position based on the column */
- {
- *(destPtr /* Begin in the lower left corner */
- - (column * textHeightBytes) /* Subtract, move up for each row */
- + (row >> 3)) /* Calculate a bit position for the column */
- |= (0x80 >> (row & 7)); /* Set a bit based on the column */
- }
- }
- else // clockwise
- {
- /* We are rotating right (clock-wise) thus */
- /* as we search across the top of the horizontal pixel bit map, */
- /* we write down the left edge of the vertical pixel bit map */
-
- if( *(srcPtr /* Begin with the upper left corner */
- + (row * textWidthBytes) /* Move down a row */
- + (column >> 3)) /* Go to the first even byte */
- & (0x80 >> (column & 7)) ) /* calculate a shifted bit position based on the column */
- {
- *(destPtr /* Begin in the lower left corner */
- + (column * textHeightBytes) /* Add, move down for each row */
- + ((textHeightBits - (row + 1)) >> 3)) /* Calculate a byte position for the column */
- |= (0x80 >> /* Set a bit based on the column */
- (((textHeightBits - 1) - row) & 7));
- }
- }
- }
- }
-
- // clean up the used source BitMap, and restore the ports properly
- DisposePtr( srcPort.portBits.baseAddr );
- ClosePort( &srcPort );
- SetPort( savePort );
-
- return( noErr );
-
- } /* GetRotatedStringBitmap */
-
-
-